﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of TextRectangleCtrl class
	/// A rectabgle with a border containing a single line of text
	/// Used throughout plugins to show blue text in a white box, design 
	/// provided by FRL
	/// </summary>
	public partial class TextRectangleCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Dependency properties

		public static readonly DependencyProperty InnerHorizontalAlignmentProperty;

		public static readonly DependencyProperty InnerMarginProperty;

		public static readonly DependencyProperty TextWrappingProperty;

		public static readonly DependencyProperty TextBrushProperty;

		public static readonly DependencyProperty BackgroundBrushProperty;

		public static readonly DependencyProperty BorderBrushProperty;

		public static readonly DependencyProperty BorderThicknessProperty;

		/////////////////////////////////////////////////////////////
		// Attributes

		public string Text
		{
			get { return tbText.Text; }
			set { tbText.Text = value; }
		}

		public HorizontalAlignment InnerHorizontalAlignment
		{
			get
			{
				return (HorizontalAlignment)GetValue(InnerHorizontalAlignmentProperty);
			}
			set
			{
				SetValue(InnerHorizontalAlignmentProperty, value);
			}
		}

		public Thickness InnerMargin
		{
			get
			{
				return (Thickness)GetValue(InnerMarginProperty);
			}
			set
			{
				SetValue(InnerMarginProperty, value);
			}
		}

		public Brush TextBrush
		{
			get
			{
				return (Brush)GetValue(TextBrushProperty);
			}
			set
			{
				SetValue(TextBrushProperty, value);
			}
		}

		public Brush BackgroundBrush
		{
			get
			{
				return (Brush)GetValue(BackgroundBrushProperty);
			}
			set
			{
				SetValue(BackgroundBrushProperty, value);
			}
		}

		public new Brush BorderBrush
		{
			get
			{
				return (Brush)GetValue(BorderBrushProperty);
			}
			set
			{
				SetValue(BorderBrushProperty, value);
			}
		}

		public new double BorderThickness
		{
			get
			{
				return (double)GetValue(BorderThicknessProperty);
			}
			set
			{
				SetValue(BorderThicknessProperty, value);
			}
		}

		/////////////////////////////////////////////////////////////
		// Construction

		static TextRectangleCtrl()
		{

			// Register dependency properties
			InnerHorizontalAlignmentProperty = DependencyProperty.Register(
				"InnerHorizontalAlignment", 
				typeof(HorizontalAlignment),
				typeof(TextRectangleCtrl),
				new PropertyMetadata(HorizontalAlignment.Center,InnerHorizontalAlignmenChanged)
			);

			InnerMarginProperty = DependencyProperty.Register(
				"InnerMargin", 
				typeof(Thickness), 
				typeof(TextRectangleCtrl),
				new PropertyMetadata(new Thickness(0,0,0,0),InnerMarginChanged)
			);
			
			TextWrappingProperty = DependencyProperty.Register(
				"TextWrapping",
				typeof(bool),
				typeof(TextRectangleCtrl),
				new PropertyMetadata(false, TextWrappingChanged)
			);

			TextBrushProperty = DependencyProperty.Register(
				"TextBrush",
				typeof(Brush),
				typeof(TextRectangleCtrl),
				new PropertyMetadata(new SolidColorBrush(Colors.White), TextBrushChanged)
			);

			BackgroundBrushProperty = DependencyProperty.Register(
				"BackgroundBrush",
				typeof(Brush),
				typeof(TextRectangleCtrl),
				new PropertyMetadata(new SolidColorBrush(Colors.Black), BackgroundBrushChanged)
			);

			BorderBrushProperty = DependencyProperty.Register(
				"BorderBrush",
				typeof(Brush),
				typeof(TextRectangleCtrl),
				new PropertyMetadata(new SolidColorBrush(Colors.Magenta), BorderBrushChanged)
			);

			BorderThicknessProperty = DependencyProperty.Register(
				"BorderThickness",
				typeof(double),
				typeof(TextRectangleCtrl),
				new PropertyMetadata((double)4, BorderThicknessChanged)
			);

		}

		public TextRectangleCtrl()
		{

			// Load xaml resources
			InitializeComponent();

			// Set default text
#if DEBUG
			this.Text = "tbText";
#endif // #if DEBUG

		}

		/////////////////////////////////////////////////////////////
		// Dependency property - Event handlers

		private static void InnerHorizontalAlignmenChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Set inner alignment
			((TextRectangleCtrl)element)._setInnerHorizontalAlignmen((HorizontalAlignment)e.NewValue);

		}

		private static void InnerMarginChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Set text margin
			((TextRectangleCtrl)element)._setInnerMarginChanged((Thickness)e.NewValue);

		}

		private static void TextWrappingChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Show or hide the border			
			((TextRectangleCtrl)element)._setTextWrappingChanged((bool)e.NewValue);

		}

		private static void TextBrushChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Call sibling function	
			((TextRectangleCtrl)element)._setTextBrushChanged((Brush)e.NewValue);

		}

		private static void BackgroundBrushChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Call sibling function	
			((TextRectangleCtrl)element)._setBackgroundBrushChanged((Brush)e.NewValue);

		}

		private static void BorderBrushChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Call sibling function	
			((TextRectangleCtrl)element)._setBorderBrushChanged((Brush)e.NewValue);

		}

		private static void BorderThicknessChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
		{

			// Call sibling function	
			((TextRectangleCtrl)element)._setBorderThicknessChanged((double)e.NewValue);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setInnerHorizontalAlignmen(HorizontalAlignment alignment)
		{

			// Set inner alignment
			tbText.HorizontalAlignment = alignment;

		}

		private void _setInnerMarginChanged(Thickness thickness)
		{

			// Set text margin
			tbText.Margin = thickness;

		}

		private void _setTextWrappingChanged(bool bWrap)
		{

			// Enable or disable wrapping
			tbText.TextWrapping = bWrap ? TextWrapping.Wrap : TextWrapping.NoWrap;

		}

		private void _setTextBrushChanged(Brush brushText)
		{

			// Update controls
			tbText.Foreground = brushText;

		}

		private void _setBackgroundBrushChanged(Brush brushBackground)
		{

			// Update controls
			LayoutRoot.Background = brushBackground;

		}

		private void _setBorderBrushChanged(Brush brushBorder)
		{

			// Update controls
			border.BorderBrush = brushBorder;

		}

		private void _setBorderThicknessChanged(double dblThickness)
		{

			// Update controls
			border.BorderThickness = new Thickness(dblThickness);
			border.Visibility = (dblThickness > 0) ? Visibility.Visible : Visibility.Collapsed;

		}

	}

}
